home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / a-reatim.adb < prev    next >
Text File  |  1996-01-30  |  7KB  |  266 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                 GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                          A D A . R E A L _ T I M E                       --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.15 $                             --
  10. --                                                                          --
  11. --       Copyright (c) 1991,1992,1993,1994, FSU, All Rights Reserved        --
  12. --                                                                          --
  13. -- GNARL is free software; you can redistribute it  and/or modify it  under --
  14. -- terms  of  the  GNU  Library General Public License  as published by the --
  15. -- Free Software  Foundation;  either version 2, or (at  your  option)  any --
  16. -- later  version.  GNARL is distributed  in the hope that  it will be use- --
  17. -- ful, but but WITHOUT ANY WARRANTY;  without even the implied warranty of --
  18. -- MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Gen- --
  19. -- eral Library Public License  for more details.  You should have received --
  20. -- a  copy of the GNU Library General Public License along with GNARL;  see --
  21. -- file COPYING.LIB.  If not,  write to the  Free Software Foundation,  675 --
  22. -- Mass Ave, Cambridge, MA 02139, USA.                                      --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Task_Clock;
  27. --  Used for, Time definitions and operations.
  28.  
  29. with System.Task_Clock.Machine_Specifics;
  30. --  Used for, Task_Clock.Machine_Specifics.Clock
  31.  
  32. with Unchecked_Conversion;
  33.  
  34. package body Ada.Real_Time is
  35.  
  36.    use System.Task_Clock;
  37.    --  for time operators.
  38.  
  39.    -----------
  40.    -- Clock --
  41.    -----------
  42.  
  43.    function Clock return Time is
  44.    begin
  45.       return Time (Task_Clock.Machine_Specifics.Clock);
  46.    end Clock;
  47.  
  48.    ---------
  49.    -- "<" --
  50.    ---------
  51.  
  52.    function "<" (Left, Right : Time) return Boolean is
  53.    begin
  54.       return Task_Clock.Stimespec (Left) < Task_Clock.Stimespec (Right);
  55.    end "<";
  56.  
  57.    function "<" (Left, Right : Time_Span) return Boolean is
  58.    begin
  59.       return Task_Clock.Stimespec (Left) < Task_Clock.Stimespec (Right);
  60.    end "<";
  61.  
  62.    ---------
  63.    -- ">" --
  64.    ---------
  65.  
  66.    function ">" (Left, Right : Time) return Boolean is
  67.    begin
  68.       return Right < Left;
  69.    end ">";
  70.  
  71.    function ">" (Left, Right : Time_Span) return Boolean is
  72.    begin
  73.       return Right < Left;
  74.    end ">";
  75.  
  76.    ----------
  77.    -- "<=" --
  78.    ----------
  79.  
  80.    function "<=" (Left, Right : Time) return Boolean is
  81.    begin
  82.       return not (Left > Right);
  83.    end "<=";
  84.  
  85.    function "<=" (Left, Right : Time_Span) return Boolean is
  86.    begin
  87.       return not (Left > Right);
  88.    end "<=";
  89.  
  90.    ----------
  91.    -- ">=" --
  92.    ----------
  93.  
  94.    function ">=" (Left, Right : Time) return Boolean is
  95.    begin
  96.       return not (Left < Right);
  97.    end ">=";
  98.  
  99.    function ">=" (Left, Right : Time_Span) return Boolean is
  100.    begin
  101.       return not (Left < Right);
  102.    end ">=";
  103.  
  104.    ---------
  105.    -- "+" --
  106.    ---------
  107.  
  108.    --  Note that Constraint_Error may be propagated
  109.  
  110.    function "+" (Left : Time; Right : Time_Span) return Time is
  111.    begin
  112.       return Time (Task_Clock.Stimespec (Left) + Task_Clock.Stimespec (Right));
  113.    end "+";
  114.  
  115.    function "+"  (Left : Time_Span; Right : Time) return Time is
  116.    begin
  117.       return Right + Left;
  118.    end "+";
  119.  
  120.    function "+"  (Left, Right : Time_Span) return Time_Span is
  121.    begin
  122.       return Time_Span (Time (Right) + Left);
  123.    end "+";
  124.  
  125.    ---------
  126.    -- "-" --
  127.    ---------
  128.  
  129.    --  Note that Constraint_Error may be propagated
  130.  
  131.    function "-"  (Left : Time; Right : Time_Span) return Time is
  132.    begin
  133.       return Time (Task_Clock.Stimespec (Left) - Task_Clock.Stimespec (Right));
  134.    end "-";
  135.  
  136.    function "-"  (Left, Right : Time) return Time_Span is
  137.    begin
  138.       return Time_Span (Left - Time_Span (Right));
  139.    end "-";
  140.  
  141.    function "-"  (Left, Right : Time_Span) return Time_Span is
  142.    begin
  143.       return Time_Span (Time (Left) - Right);
  144.    end "-";
  145.  
  146.    function "-"  (Right : Time_Span) return Time_Span is
  147.    begin
  148.       return Time_Span_Zero - Right;
  149.    end "-";
  150.  
  151.    ---------
  152.    -- "/" --
  153.    ---------
  154.  
  155.    --  Note that Constraint_Error may be propagated
  156.  
  157.    function "/"  (Left, Right : Time_Span) return integer is
  158.    begin
  159.       return Task_Clock.Stimespec (Left) / Task_Clock.Stimespec (Right);
  160.    end "/";
  161.  
  162.    function "/"  (Left : Time_Span; Right : Integer) return Time_Span is
  163.    begin
  164.       return Time_Span (Task_Clock.Stimespec (Left) / Right);
  165.    end "/";
  166.  
  167.    ---------
  168.    -- "*" --
  169.    ---------
  170.  
  171.    --  Note that Constraint_Error may be propagated
  172.  
  173.    function "*"  (Left : Time_Span; Right : Integer) return Time_Span is
  174.    begin
  175.       return Time_Span (Task_Clock.Stimespec (Left) * Right);
  176.    end "*";
  177.  
  178.    function "*"  (Left : Integer; Right : Time_Span) return Time_Span is
  179.    begin
  180.       return Right * Left;
  181.    end "*";
  182.  
  183.    -----------
  184.    -- "abs" --
  185.    -----------
  186.  
  187.    --  Note that Constraint_Error may be propagated
  188.  
  189.    function "abs" (Right : Time_Span) return Time_Span is
  190.    begin
  191.       if Right < Time_Span_Zero then
  192.          return -Right;
  193.       end if;
  194.  
  195.       return Right;
  196.    end "abs";
  197.  
  198.    -----------------
  199.    -- To_Duration --
  200.    -----------------
  201.  
  202.    function To_Duration (FD : Time_Span) return Duration is
  203.    begin
  204.       return Task_Clock.Stimespec_To_Duration (Task_Clock.Stimespec (FD));
  205.    end To_Duration;
  206.  
  207.    ------------------
  208.    -- To_Time_Span --
  209.    ------------------
  210.  
  211.    function To_Time_Span (D : Duration) return Time_Span is
  212.    begin
  213.       return Time_Span (Task_Clock.Duration_To_Stimespec (D));
  214.    end To_Time_Span;
  215.  
  216.    -----------------
  217.    -- Nanoseconds --
  218.    -----------------
  219.  
  220.    function Nanoseconds (NS : integer) return Time_Span is
  221.    begin
  222.       return Time_Span_Unit * NS;
  223.    end Nanoseconds;
  224.  
  225.    ------------------
  226.    -- Microseconds --
  227.    ------------------
  228.  
  229.    function Microseconds  (US : integer) return Time_Span is
  230.    begin
  231.       return Nanoseconds (US) * 1000;
  232.    end Microseconds;
  233.  
  234.    -------------------
  235.    --  Milliseconds --
  236.    -------------------
  237.  
  238.    function Milliseconds (MS : integer) return Time_Span is
  239.    begin
  240.       return Microseconds (MS) * 1000;
  241.    end Milliseconds;
  242.  
  243.    -----------
  244.    -- Split --
  245.    -----------
  246.  
  247.    --  D is nonnegative Time_Span
  248.  
  249.    procedure Split (T : Time; S : out Seconds_Count; D : out Time_Span) is
  250.    begin
  251.       S := Seconds_Count
  252.         (Task_Clock.Stimespec_Seconds (Task_Clock.Stimespec (T)));
  253.       D := T - Time_Of (S, Time_Span_Zero);
  254.    end Split;
  255.  
  256.    -------------
  257.    -- Time_Of --
  258.    -------------
  259.  
  260.    function Time_Of (S : Seconds_Count; D : Time_Span) return Time is
  261.    begin
  262.       return (Time (Task_Clock.Time_Of (Integer (S), 0)) + D);
  263.    end Time_Of;
  264.  
  265. end Ada.Real_Time;
  266.